"use strict";
cc._RF.push(module, 'f0043cW/o1BGJvxtmvPNpyZ', 'game');
// script/game.js

"use strict";

cc.Class({
  "extends": cc.Component,
  properties: {
    block_show: {
      type: cc.Node,
      "default": null
    },
    block_arr: {
      type: [cc.Prefab],
      "default": []
    },
    label_score: {
      type: cc.Label,
      "default": null
    },
    pre_tx: {
      type: cc.Prefab,
      "default": null
    },
    node_xian: {
      type: cc.Node,
      "default": null
    },
    layerOver: {
      type: cc.Node,
      "default": null
    },
    audoi_pz: {
      type: cc.AudioClip,
      "default": null
    }
  },
  onLoad: function onLoad() {
    console.log('onLoad');
    window.game = this;
    this.i_game = 1; //1:大西瓜 2：小芝麻

    this.arr_num_1 = [1, 2, 3, 4, 5];
    this.arr_num_2 = [6, 7, 8, 9];
    this.init();
    this.setTouch();
    cc.director.getPhysicsManager().enabled = true;
  },
  //显示游戏名称
  showName: function showName() {
    var node_name = this.node.getChildByName('label_name');
    var node_btnGame = this.node.getChildByName('btn_game');

    if (this.i_game == 1) {
      node_name.getComponent(cc.Label).string = '合成大西瓜';
      node_btnGame.getChildByName('Background').getChildByName('Label').getComponent(cc.Label).string = '小芝麻';
    } else {
      node_name.getComponent(cc.Label).string = '合成小芝麻';
      node_btnGame.getChildByName('Background').getChildByName('Label').getComponent(cc.Label).string = '大西瓜';
    }
  },
  init: function init() {
    this.num_buShu = 0;
    this.block_show.active = false;
    this.node_xian.active = false;
    this.layerOver.active = false;
    this.layerOver.zIndex = 9;
    this.f_scale = 0.6;
    this.block_show.scale = this.f_scale;
    this.score_curr = 0; //当前分数

    this.f_xian = 300; //警戒线显示的高度

    this.is_over = false;
    this.label_score.string = this.score_curr;

    if (this.i_game == 1) {
      //大
      this.block_random = 1;
    } else {
      //小
      this.block_random = 10;
    }

    this.showBlock();
    this.cleanAllBlocks();
    this.showName();
  },
  playSound: function playSound() {
    cc.audioEngine.play(this.audoi_pz, false, 1);
  },
  //屏幕触摸
  setTouch: function setTouch() {
    // 使用事件名来注册
    this.node.on('touchstart', function (event) {
      if (this.block_show.active == false || this.is_over) {
        return;
      }

      var pos_touch = event.getLocation();
      pos_touch = this.node.convertToNodeSpaceAR(pos_touch);
      this.block_show.x = pos_touch.x;
    }, this);
    this.node.on('touchmove', function (event) {
      if (this.block_show.active == false || this.is_over) {
        return;
      }

      var pos_touch = event.getLocation();
      pos_touch = this.node.convertToNodeSpaceAR(pos_touch);
      this.block_show.x = pos_touch.x;
    }, this);
    this.node.on('touchend', function (event) {
      if (this.block_show.active == false || this.is_over) {
        return;
      }

      this.block_show.active = false;
      var pos_blockShow = this.block_show.getPosition();
      this.createBlock(this.block_random, pos_blockShow, false);

      if (this.i_game == 1) {
        //大
        var arr_length = this.arr_num_1.length;
        var i_arrRandom = Math.floor(Math.random() * arr_length);
        this.block_random = this.arr_num_1[i_arrRandom];

        if (this.num_buShu <= 1) {
          this.block_random = 1;
        }
      } else {
        //小
        var arr_length = this.arr_num_2.length;
        var i_arrRandom = Math.floor(Math.random() * arr_length);
        this.block_random = this.arr_num_2[i_arrRandom];

        if (this.num_buShu <= 1) {
          this.block_random = 10;
        }
      }

      this.scheduleOnce(function () {
        this.showBlock();
      }.bind(this), 1);
    }, this);
    this.node.on('touchcancel', function (event) {
      console.log('touchcancel');

      if (this.block_show.active == false || this.is_over) {
        return;
      }

      this.block_show.active = false;
      var pos_blockShow = this.block_show.getPosition();
      this.createBlock(this.block_random, pos_blockShow, false);

      if (this.i_game == 1) {
        //大
        var arr_length = this.arr_num_1.length;
        var i_arrRandom = Math.floor(Math.random() * arr_length);
        this.block_random = this.arr_num_1[i_arrRandom];

        if (this.num_buShu <= 1) {
          this.block_random = 1;
        }
      } else {
        //小
        var arr_length = this.arr_num_2.length;
        var i_arrRandom = Math.floor(Math.random() * arr_length);
        this.block_random = this.arr_num_2[i_arrRandom];

        if (this.num_buShu <= 1) {
          this.block_random = 10;
        }
      }

      this.scheduleOnce(function () {
        this.showBlock();
      }.bind(this), 1);
    }, this);
  },
  //显示生成的元素块
  showBlock: function showBlock() {
    this.block_show.active = true;
    this.block_show.setPosition(cc.v2(0, 530));
    var children = this.block_show.children;

    for (var i = 0; i < children.length; i++) {
      if (children[i].name == this.block_random) {
        children[i].active = true;
      } else {
        children[i].active = false;
      }
    }

    this.block_show.scale = 0;
    var act_1 = cc.scaleTo(0.15, this.f_scale);
    this.block_show.runAction(act_1);
  },
  //创建元素块
  createBlock: function createBlock(i_type, pos_touch, can_scale) {
    this.num_buShu++;
    var node_block = cc.instantiate(this.block_arr[i_type]); //实例化

    node_block.parent = this.node;
    node_block.scale = this.f_scale;
    node_block.setPosition(pos_touch);
    var js_block = node_block.getComponent('block');
    js_block.init(i_type);

    if (can_scale) {
      node_block.scale = 0.3;
      var act_1 = cc.scaleTo(0.12, this.f_scale);
      node_block.runAction(act_1);
    }
  },
  //创建爆炸特效
  createTx: function createTx(i_type, pos_block, block_wh) {
    var node_tx = cc.instantiate(this.pre_tx); //实例化

    node_tx.parent = this.node;
    node_tx.scale = 0;
    node_tx.zIndex = 3;
    node_tx.width = block_wh;
    node_tx.height = block_wh;
    node_tx.setPosition(pos_block);

    if (i_type == 1) {
      node_tx.color = new cc.Color(110, 16, 100);
    } else if (i_type == 2) {
      node_tx.color = new cc.Color(255, 9, 36);
    } else if (i_type == 3) {
      node_tx.color = new cc.Color(253, 111, 1);
    } else if (i_type == 4) {
      node_tx.color = new cc.Color(255, 230, 23);
    } else if (i_type == 5) {
      node_tx.color = new cc.Color(93, 222, 31);
    } else if (i_type == 6) {
      node_tx.color = new cc.Color(229, 25, 50);
    } else if (i_type == 7) {
      node_tx.color = new cc.Color(245, 159, 98);
    } else if (i_type == 8) {
      node_tx.color = new cc.Color(255, 225, 69);
    } else if (i_type == 9) {
      node_tx.color = new cc.Color(205, 201, 189);
    } else if (i_type == 10) {
      node_tx.color = new cc.Color(248, 64, 100);
    }

    var act_1 = cc.scaleTo(0.16, 1);
    var act_2 = cc.callFunc(function () {
      node_tx.removeFromParent();
    });
    var end = cc.sequence(act_1, act_2);
    node_tx.runAction(end);
  },
  //增加分数
  addScore: function addScore(i_score) {
    this.score_curr = this.score_curr + i_score;
    this.label_score.string = this.score_curr;
  },
  //显示警戒线
  showXian: function showXian() {
    var can_show = false;
    var children = this.node.children;

    for (var i = 0; i < children.length; i++) {
      var js_block = children[i].getComponent('block');

      if (js_block && js_block.is_pz) {
        var y_block = children[i].y;
        var y_gao = y_block + children[i].height / 2 * this.f_scale;

        if (y_gao > this.f_xian) {
          can_show = true;
          break;
        }
      }
    }

    if (can_show) {
      this.node_xian.active = true;
    } else {
      this.node_xian.active = false;
    }
  },
  //游戏结束
  gameOver: function gameOver() {
    var _this = this;

    this.node_xian.active = false;
    this.block_show.active = false;
    this.layerOver.getChildByName('label_score').getComponent(cc.Label).string = this.score_curr;
    var num_bestScore = cc.sys.localStorage.getItem('num_bestScore');

    if (!num_bestScore) {
      num_bestScore = 0;
    }

    if (num_bestScore < this.score_curr) {
      num_bestScore = this.score_curr;
    }

    this.layerOver.getChildByName('label_bestScore').getComponent(cc.Label).string = num_bestScore;
    cc.sys.localStorage.setItem('num_bestScore', num_bestScore);
    var f_timeLong = 0;
    this.is_over = true;
    var children = this.node.children;

    var _loop = function _loop(i) {
      js_block = children[i].getComponent('block');

      if (js_block) {
        f_time = 0.1 + 0.025 * i;

        if (f_timeLong < f_time) {
          f_timeLong = f_time;
        }

        act_1 = cc.delayTime(f_time);
        act_2 = cc.callFunc(function () {
          var js_block = children[i].getComponent('block');
          var block_type = js_block.block_type;
          var pos_block = children[i].getPosition();
          var block_wh = children[i].width;
          this.createTx(block_type, pos_block, block_wh);
          this.addScore(block_type);
          children[i].active = false;
        }, _this);
        end = cc.sequence(act_1, act_2);
        children[i].runAction(end);
      }
    };

    for (var i = 0; i < children.length; i++) {
      var js_block;
      var f_time;
      var act_1;
      var act_2;
      var end;

      _loop(i);
    }

    this.scheduleOnce(function () {
      this.layerOver.active = true;
    }, f_timeLong + 0.1);
  },
  //清空所有的元素块
  cleanAllBlocks: function cleanAllBlocks() {
    var children = this.node.children;

    for (var i = children.length - 1; i >= 0; i--) {
      var js_block = children[i].getComponent('block');

      if (js_block) {
        children[i].removeFromParent();
      }
    }
  },
  //按钮回调
  btnCallBack: function btnCallBack(sender, str) {
    if (str == 'btn_rePlay') {
      //重新开始游戏
      this.init();
    } else if (str == 'btn_game') {
      if (this.i_game == 1) {
        this.i_game = 2;
      } else {
        this.i_game = 1;
      }

      this.init();
    }
  },
  update: function update(dt) {
    //每秒运行60次
    //console.log('update:'+dt);
    this.showXian();
  }
});

cc._RF.pop();